﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UtilZ.Dotnet.Ex.DataStruct
{
    /// <summary>
    /// 回调执行定时任务
    /// </summary>
    public class CallbackExcuteTimingTask : TimingTaskAbs
    {
        /// <summary>
        /// 回调
        /// </summary>
        private readonly Action<CallbackExcuteTimingTask> _callback;


        /// <summary>
        /// 构造函数-间隔执行
        /// </summary>
        /// <param name="callback">回调执行</param>
        /// <param name="interval"></param>
        /// <param name="count">执行次数,小于0表示无限,大于等于0表示执行指定次数</param>
        public CallbackExcuteTimingTask(Action<CallbackExcuteTimingTask> callback, TimeSpan interval,  int count = -1)
            : base(interval, count)
        {
            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            this._callback = callback;
        }

        /// <summary>
        /// 构造函数-定时执行
        /// </summary>
        /// <param name="callback">回调执行</param>
        /// <param name="hour">时</param>
        /// <param name="minute">分</param>
        /// <param name="second">秒</param>
        /// <param name="millisecond">毫秒</param>
        /// <param name="count">执行次数,小于0表示无限,大于等于0表示执行指定次数</param>
        public CallbackExcuteTimingTask(Action<CallbackExcuteTimingTask> callback, int hour, int minute, int second, int millisecond, int count = -1)
             : base(hour, minute, second, millisecond, count)
        {
            if (callback == null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            this._callback = callback;
        }



        /// <summary>
        /// 执行任务
        /// </summary>
        protected override void PrimitiveExcute()
        {
            this._callback(this);
        }


    }
}
